import traceback

from enum import Enum


class TaskStatusName(Enum):
    scheduled = 'TS_SCHEDULED'
    queued = 'TS_QUEUED'
    processing = 'TS_PROCESSING'
    completed = 'TS_COMPLETED'
    failed = 'TS_FAILED'
    aborted = 'TS_ABORTED'
    aborting = 'TS_ABORTING'


class SessionStatusName(Enum):
    scheduled = 'TSS_SCHEDULED'
    queued = 'TSS_QUEUED'
    processing = 'TSS_PROCESSING'
    completed = 'TSS_COMPLETED'
    failed = 'TSS_FAILED'
    aborted = 'TSS_ABORTED'
    aborting = 'TS_ABORTING'


class TaskType(Enum):
    securityEvent = 'securityEvent'
    vul = 'vul'
    availability = 'availability'
    risk_link = 'risk_link'
    change_check = 'change_check'


class EXTaskType(Enum):
    vul_verification_task = 'vul_verification_task'
    target_info = 'target_info'
    change_check_config = 'change_check_config'


class ConfidenceMap(object):
    def get(self, confidence):
        try:
            if confidence >= 80:
                return 4
            elif confidence >= 60:
                return 3
            else:
                return 2
        except Exception:
            traceback.print_exc()

            return 0


CONFIDENCE_MAP = ConfidenceMap()


class ConfidenceScoreMap(object):
    def get(self, confidence):
        return 1


CONFIDENCE_SCORE_MAP = ConfidenceScoreMap()


SEVERITY_MAP = {
    0: 'UNKNOWN',
    1: 'INFO',
    2: 'LOW',
    3: 'MEDIUM',
    4: 'HIGH',
    5: 'CRITICAL',
}

DEFAULT_SECONDS = {
    'vul': 60 * 60,
    'securityEvent': 30 * 60,
    'availability': 15 * 60,
    'risk_link': 30 * 60,
    'change_check': 30 * 60
}

CONFIDENCE_INTERVAL = {80: SEVERITY_MAP[4], 60: SEVERITY_MAP[3], 0: SEVERITY_MAP[2]}

SEVERITY_SCORE_MAP = {
    0: (-1, -1),
    1: (0, 0),
    2: (1, 25),
    3: (26, 50),
    4: (51, 75),
    5: (76, 100),
}

RISK_TYPE_MAP = {0: 'unknown', 1: 'icp', 2: 'keyword', 3: 'domain'}


# reverse map
REVERSE_SEVERITY_MAP = {v: k for k, v in SEVERITY_MAP.items()}

# [80, 101)
ReverseConfidenceMap = {
    SEVERITY_MAP[2]: (0, 60),
    SEVERITY_MAP[3]: (60, 80),
    SEVERITY_MAP[4]: (80, 101),
}


class ScanProfile(Enum):
    """enum ScanProfile {
        SP_DEFAULT = 0;
        SP_FULL = 1;
        SP_HIGH_RISK = 2;
        SP_SQL_INJECTION = 3;
        SP_XSS = 4;
        SP_NORMAL = 5;
        SP_QUICK = 6;
        }

    Args:
        Enum ([type]): [description]
    """

    high = 2
    medium = 1
    low = 1


class SecurityEventPlugins(Enum):
    black_links = 'black_links'
    cryptojacking = 'cryptojacking'
    keyword = 'keyword'
    malscan = 'malscan'
    seo_hijack = 'seo_hijack'
    broken_links = 'broken_links'
    privacy_disclosure = 'privacy_disclosure'


SECURITYEVENT_PLUGINS_TUPLE = tuple(item.value for item in SecurityEventPlugins)


AvailabilityPlugins = [
    "http_get",
    "ping",
]


class AvailabilityNormalStatus(Enum):
    default = 0
    normal = 1
    abnormal = 2


DEFAULT_CLOUD_ABNORMAL_NODES = 3

CRAWLER_SCOPE = (2, 3, 4, 5)

UTC_ISO_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
DATETIME_FORMAT_2 = "%Y%m%d%H%M%S"
DATE_FORMAT_2 = "%Y%m%d"
DATE_FORMAT = "%Y-%m-%d"


class AuditLogAction(Enum):
    create = 'create'
    add = 'add'
    up = 'up'
    del_ = 'del'


class LogModuleName(Enum):
    monitor_list = 'monitor_list'  # 监测列表
    task_management = 'task_manage'  # 任务管理
    vuln_db = 'vuln_db'  # 漏洞库
    warn_center = 'warn_center'  # 告警中心
    log_center = 'log_center'  # 日志中心
    policy_management = 'policy_management'  # 策略管理
    system_management = 'system_management'  # 系统状态
    user_management = 'account_manager'  # 用户管理
    login = 'login'  # 登陆
    system = 'system'  # 系统日志
    net_management = 'net_management'  # 网络配置
    backup = 'backup'


class LogTypeName(Enum):
    task = 'task'  # 任务
    target_group = 'target_group'  # 任务组
    target = 'target'  # 业务系统
    login = 'login'  # 登陆
    logout = 'logout'  # 退出
    system = 'system'  # 系统日志
    log_management = 'log_management'
    profile_template = 'profile_template'
    system_network = 'system_network'
    system_dns = 'system_dns'
    snmp = 'snmp'
    remote_assistance = 'remote_assistance'
    route = 'route'
    turn_off_device = 'turn_off_device'
    restart_device = "restart_device"
    ntp = 'ntp'
    keyword = 'keyword'
    warn_center = 'warn_center'
    backup = 'backup'
    account = 'account'
    system_upgrade = 'system_upgrade'
    upload_ssl = 'upload_ssl'
    permission = 'permission'
    user_group = 'user_group'
    api_token = 'api_token'
    api_url = 'api_url'
    ssh = 'ssh'
    change_check_config = 'change_check_config'


class EmailTemplate(Enum):
    detail = 'email_template_detail.html'
    common = 'email_template_common.html'
    table = 'email_template_table.html'
    test = 'email_template_test.html'
    ddisk = 'email_template_ddisk.html'
    change_check = 'email_template_change_check.html'


class ScanJobName(Enum):
    site_info = 'site_info'
    appscan = 'appscan'
    kscan = 'kscan'
    nscan = 'nscan'


class ScanEventName(Enum):
    statistics = 'statistics'


LANGUAGE_MAP = {'zh-hans': 'zh_cn', 'zh-cn': 'zh_cn', 'en-us': 'en_us'}


class PreLoginMethod(Enum):
    form = 'form'
    json = 'json'
    cookie = 'cookie'
    headers = 'headers'


class NetWorkType(Enum):
    v4 = 1
    v6 = 2
    v4v6 = 3


def get_language_code(lan):
    return LANGUAGE_MAP[lan]


class PictureName(Enum):
    index_logo = 'index_logo'
    index_bg = 'index_bg'
    ui_logo = 'ui_logo'
    title_logo = 'title_logo'
    report_index_bg = 'report_index_bg'
    report_end_bg = 'report_end_bg'
    report_logo = 'report_logo'
    report_end_qr = 'report_end_qr'
    email_logo = 'email_logo'


class ReportTaskType(Enum):
    last_task = 'last_task'
    time_period_task = 'time_period_task'
    history_task = 'history_task'


class ReportExportStatus(Enum):
    waiting = 'waiting'
    pending = 'pending'
    success = 'success'
    failed = 'failed'
